﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Telerik.Windows.Controls.GridView;

namespace StatusPlan3.UC
{
    /// <summary>
    /// Interaction logic for UCProject.xaml
    /// </summary>
    public partial class UCProjectGroup : UserControl
    {
        public ProjectGroup projectGroup;

        public UCProjectGroup()
        {
            InitializeComponent();
        }

        public void setData(ProjectGroup pg)
        {
            projectGroup = StatusPlanData.statusPlanHandler.getProjectGroup(pg.id);
            populateFrame();
        }

        public void selectedRow(MouseEventArgs e)
        {
            
        }

        public void showTypes()
        {
            TabItem_Types.IsSelected = true;
        }

        public void showMembers()
        {
            TabItem_Members.IsSelected = true;
        }

        public void showPriorities()
        {
            TabItem_Priorities.IsSelected = true;
        }

        public void showStates()
        {
            TabItem_States.IsSelected = true;
        }

        public void showInfo()
        {
            TabItem_Info.IsSelected = true;
        }

        public void showProjects()
        {
            TabItem_Projects.IsSelected = true;
        }

        public void populateFrame()
        {
            projectGroup = StatusPlanData.statusPlanHandler.getProjectGroup(projectGroup.id);

            TextBlock_Created.Text = "Stovnað: " + projectGroup.createdDate.ToString("dd-MM-yyyy HH:mm");

            TextBlock_CreatedBy.Text = "Stavnað av: " + projectGroup.createdBy.fullName;
            
            if (projectGroup.planedEndDate != null)
                TextBlock_PlanedEndDate.Text = "Ætla slut dagur: " + projectGroup.planedEndDate.Value.ToString("dd-MM-yyyy HH:mm");
            else
                TextBlock_PlanedEndDate.Text = "Ætla slut dagur: Ikki ásett";

            TextBlock_ProjectGroupName.Text = "Navn: " + projectGroup.name ;

            if(projectGroup.startDate != null)
                TextBlock_StartDate.Text = "Byrja: " + projectGroup.startDate.Value.ToString("dd-MM-yyyy HH:mm");
            else
                TextBlock_StartDate.Text = "Byrja: ikki ásett";

            projectGridView.ItemsSource = StatusPlanData.statusPlanHandler.getProjects(projectGroup.id);


            memberGridView.ItemsSource = projectGroup.projectMembers;
            typeGridView.ItemsSource = projectGroup.types;
            issueTypeGridView.ItemsSource = projectGroup.issueTypes;

            statesGridView.ItemsSource = projectGroup.statuses;
            issuestatesGridView.ItemsSource = projectGroup.issueStatuses;

            prioritiesGridView.ItemsSource = projectGroup.priorities;
        }

        internal void refreshFrame()
        {
            populateFrame();
        }

        private void projectGridView_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
        {

        }

       

        private void typeGridView_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
        {
            Type_Button_Edit.IsEnabled = false;
            Type_Button_Delete.IsEnabled = false;

            Type t = typeGridView.SelectedItem as Type;
            if (t != null)
            {
                Type_Button_Edit.IsEnabled = true;
                Type_Button_Delete.IsEnabled = true;
            }
        }

        private void issueTypeGridView_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
        {
            IssueType_Button_Edit.IsEnabled = false;
            IssueType_Button_Delete.IsEnabled = false;

            IssueType t = issueTypeGridView.SelectedItem as IssueType;
            if (t != null)
            {
                IssueType_Button_Edit.IsEnabled = true;
                IssueType_Button_Delete.IsEnabled = true;
            }
        }

        private void statesGridView_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
        {
            State_Button_Edit.IsEnabled = false;
            State_Button_Delete.IsEnabled = false;

            Status t = statesGridView.SelectedItem as Status;
            if (t != null)
            {
                State_Button_Edit.IsEnabled = true;
                State_Button_Delete.IsEnabled = true;
            }
        }

        private void issuestatesGridView_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
        {
            IssueState_Button_Edit.IsEnabled = false;
            IssueState_Button_Delete.IsEnabled = false;

            IssueStatus t = issuestatesGridView.SelectedItem as IssueStatus;
            if (t != null)
            {
                IssueState_Button_Edit.IsEnabled = true;
                IssueState_Button_Delete.IsEnabled = true;
            }
        }

        private void prioritiesGridView_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
        {
            Priority_Button_Edit.IsEnabled = false;
            Priority_Button_Delete.IsEnabled = false;

            Priority t = prioritiesGridView.SelectedItem as Priority;
            if (t != null)
            {
                Priority_Button_Edit.IsEnabled = true;
                Priority_Button_Delete.IsEnabled = true;
            }
        }

        private void memberGridView_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
        {
            Member_Button_Delete.IsEnabled = false;

            User t = memberGridView.SelectedItem as User;
            if (t != null)
            {
                Member_Button_Delete.IsEnabled = true;
            }
        }

        private void RadButton_Click(object sender, RoutedEventArgs e)
        {
            Window_Type wt = new Window_Type();
            wt.showNew(projectGroup);
            refreshFrame();
        }

        private void Type_Button_Edit_Click(object sender, RoutedEventArgs e)
        {
            Type t = typeGridView.SelectedItem as Type;
            if (t != null)
            {
                Window_Type wt = new Window_Type();

                wt.showEdit(projectGroup, t);
                refreshFrame();
            }
        }

        private void Type_Button_Delete_Click(object sender, RoutedEventArgs e)
        {
            Type t = typeGridView.SelectedItem as Type;
            if (t != null)
            {
                if (MessageBox.Show("Ert tú vís/ur í at tú vilt strika " + t.name + "?", "Strika", MessageBoxButton.YesNoCancel, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    StatusPlanData.statusPlanHandler.deleteTask(projectGroup, t);
                    refreshFrame();
                }
            }
        }

        private void IssueType_Button_Add_Click(object sender, RoutedEventArgs e)
        {
            Window_IssueType wt = new Window_IssueType();
            wt.showNew(projectGroup);
            refreshFrame();
        }

        private void IssueType_Button_Edit_Click(object sender, RoutedEventArgs e)
        {
            IssueType t = issueTypeGridView.SelectedItem as IssueType;
            if (t != null)
            {
                Window_IssueType wt = new Window_IssueType();

                wt.showEdit(projectGroup, t);
                refreshFrame();
            }
        }

        private void IssueType_Button_Delete_Click(object sender, RoutedEventArgs e)
        {
            IssueType t = issueTypeGridView.SelectedItem as IssueType;
            if (t != null)
            {
                if (MessageBox.Show("Ert tú vís/ur í at tú vilt strika " + t.name + "?", "Strika", MessageBoxButton.YesNoCancel, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    StatusPlanData.statusPlanHandler.deleteIssueType(projectGroup, t);
                    refreshFrame();
                }
            }
        }

        private void State_Button_Add_Click(object sender, RoutedEventArgs e)
        {
            Window_Status wt = new Window_Status();
            wt.showNew(projectGroup);
            refreshFrame();
        }

        private void State_Button_Edit_Click(object sender, RoutedEventArgs e)
        {
            Status t = statesGridView.SelectedItem as Status;
            if (t != null)
            {
                Window_Status wt = new Window_Status();

                wt.showEdit(projectGroup, t);
                refreshFrame();
            }
        }

        private void State_Button_Delete_Click(object sender, RoutedEventArgs e)
        {
            Status t = statesGridView.SelectedItem as Status;
            if (t != null)
            {
                if (MessageBox.Show("Ert tú vís/ur í at tú vilt strika " + t.name + "?", "Strika", MessageBoxButton.YesNoCancel, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    StatusPlanData.statusPlanHandler.deleteState(t, projectGroup);
                    refreshFrame();
                }
            }
        }

        private void IssueState_Button_Edit_Click(object sender, RoutedEventArgs e)
        {
            IssueStatus t = issuestatesGridView.SelectedItem as IssueStatus;
            if (t != null)
            {
                Window_IssueStatus wt = new Window_IssueStatus();

                wt.showEdit(projectGroup, t);
                refreshFrame();
            }
        }

        private void IssueState_Button_Delete_Click(object sender, RoutedEventArgs e)
        {
            IssueStatus t = issuestatesGridView.SelectedItem as IssueStatus;
            if (t != null)
            {
                if (MessageBox.Show("Ert tú vís/ur í at tú vilt strika " + t.name + "?", "Strika", MessageBoxButton.YesNoCancel, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    StatusPlanData.statusPlanHandler.deleteIssueState(t, projectGroup);
                    refreshFrame();
                }
            }
        }

        private void IssueState_Button_Add_Click(object sender, RoutedEventArgs e)
        {
            Window_IssueStatus wt = new Window_IssueStatus();
            wt.showNew(projectGroup);
            refreshFrame();
        }

        private void Priority_Button_Add_Click(object sender, RoutedEventArgs e)
        {
            Window_Priority wt = new Window_Priority();
            wt.showNew(projectGroup);
            refreshFrame();
        }

        private void Priority_Button_Edit_Click(object sender, RoutedEventArgs e)
        {
            Priority t = prioritiesGridView.SelectedItem as Priority;
            if (t != null)
            {
                Window_Priority wt = new Window_Priority();

                wt.showEdit(projectGroup, t);
                refreshFrame();
            }
        }

        private void Priority_Button_Delete_Click(object sender, RoutedEventArgs e)
        {
            Priority t = prioritiesGridView.SelectedItem as Priority;
            if (t != null)
            {
                if (MessageBox.Show("Ert tú vís/ur í at tú vilt strika " + t.name + "?", "Strika", MessageBoxButton.YesNoCancel, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    StatusPlanData.statusPlanHandler.deletePriority(t, projectGroup);
                    refreshFrame();
                }
            }
        }

        private void Member_Button_Add_Click(object sender, RoutedEventArgs e)
        {
            Window_SelectUsers wsu = new Window_SelectUsers();
            wsu.setData(projectGroup);
            refreshFrame();
        }
    }
}
